package com.party.poster.draw;



import com.party.poster.PosterElement;
import com.party.poster.entity.SamplePoster;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class PosterDefaultDraw {

    private static final List<PosterDraw> callChains;

    static {
        callChains = new ArrayList<PosterDraw>(){{
            add(new TextDraw());
            add(new ImgDraw());
            add(new GraphicalDraw());
        }};
    }

    public static BufferedImage draw(SamplePoster samplePoster){
        BufferedImage canvas = CanvasDraw.draw(samplePoster.getCanvas());
        Graphics2D graphics = canvas.createGraphics();
        List<PosterElement> elements = samplePoster.getPosterElements();
        if (elements == null || elements.isEmpty()){
            //Assert.isNotNull(elements,"海报元素为空");
            throw new RuntimeException("海报元素为空");

        }
        for (PosterElement element :elements) {
            boolean isTrue = false;
            for(PosterDraw posterDraw : callChains){
                isTrue = posterDraw.draw(graphics,element);
                if (isTrue){
                    break;
                }
            }
            if (isTrue == false){
                //Assert.isTrue(isTrue,"海报元素类型错误" + element.getClass());
                throw new RuntimeException("海报元素类型错误" + element.getClass());
            }
            graphics.dispose();
            graphics = canvas.createGraphics();
        }
        return canvas;
    }

}
